package com.goldsprite.gdxcore.entities;

import com.goldsprite.gdxcore.utils.sorts.*;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.goldsprite.utils.math.Vector2;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.Color;
//import com.goldsprite.gdxcore.physics.RectCollider;

public class TmpBlock implements IEntitySortable {
	public TextureRegion region;
	public Vector2 originPos = new Vector2();
	public Vector2 lastOriginPos = new Vector2();
	public Vector2 renderSize = new Vector2().set(32);
	public float lastZ;
	public EntitySort<TmpBlock> sorter;
	public int cachedIndex;
	//public RectCollider rcoll = new RectCollider();

	@Override
	public float z() {
		return originPos.y - renderSize.y/2;
	}

	public boolean isSort() {
		if (z() != lastZ) {
			lastZ = z();
			return true;
			/*
			 // 假设我们可以缓存 index
			 int index = cachedIndex;
			 LogViewerService.logDebug("cachedIndex: "+cachedIndex);
			 Array<TmpBlock> ents = sorter.getSortedEntities();
			 TmpBlock left = index > 0 ? ents.get(index - 1) : null;
			 TmpBlock right = index < ents.size - 1 ? ents.get(index + 1) : null;
			 LogViewerService.logDebug("z: %.2f, left-z: %s, right-z: %s", z(), (left==null?"null":MathUtils.preciNum(left.z(), 2)), (right==null?"null":MathUtils.preciNum(right.z(), 2)));
			 if ((left != null && z() > left.z()) || (right != null && z() < right.z())) {
			 return true;
			 }else{
			 //LogViewerService.logDebug("isSort: z变动但left-right无更新.");
			 }*/
		}
		return false;
	}

	public void setSorter(EntitySort<TmpBlock> sorter){
		this.sorter = sorter;
	}

	public EntitySort<TmpBlock> getSorter() {
		return sorter;
	}

	public void setCachedIndex(int index){
		this.cachedIndex = index;
	}
	
	public void draw(Batch batch){
		TmpBlock obj = this;
		float cx = obj.originPos.x;
		float cy = obj.originPos.y;
		float leftDownX = cx-obj.renderSize.x/2;
		float leftDownY = cy-obj.renderSize.y/2;
		if(region != null) batch.draw(obj.region, leftDownX, leftDownY, obj.renderSize.x, obj.renderSize.y);
	}
	
	public void drawDebug(ShapeRenderer sBatch, String type){
		TmpBlock obj = this;
		float gizmoSize = 10;
		float cx = obj.originPos.x;
		float cy = obj.originPos.y;
		float leftDownX = cx-obj.renderSize.x/2;
		float leftDownY = cy-obj.renderSize.y/2;

		sBatch.setColor(Color.WHITE);
		
		if("center".equals(type))
			sBatch.circle(cx, cy, gizmoSize);
			
		if("zPoint".equals(type))
			sBatch.circle(cx, obj.z(), gizmoSize);
			
		if(type.equals("bound"))
			sBatch.rect(leftDownX, leftDownY, obj.renderSize.x, obj.renderSize.y);
		
		/*sBatch.setColor(rcoll.collInfo.isCollision ?Color.RED :Color.GREEN);
		if(type.equals("collBound")){
			float rcollLeftDownX = rcoll.bound.leftX();
			float rcollLeftDownY = rcoll.bound.bottomY();
			sBatch.rect(rcollLeftDownX, rcollLeftDownY, rcoll.bound.halfSize.x*2, rcoll.bound.halfSize.y*2);
		}*/
	}
	
	
	public class CollInfo {
		public boolean isCollision;
		public Vector2 direction = new Vector2();
	}
}
